package com.harmonyos.cases.view;

import com.harmonyos.cases.listener.ConfirmListener;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

// 确认dialog
public class ConfirmDialog extends DialogWrapper {

    private String description = "";
    private JLabel desLabel;

    private ConfirmListener confirmListener;

    protected ConfirmDialog(String description) {
        super(true);
        this.description = description;
        setResizable(false);
//        setUndecorated(true);
        setCancelButtonText("取消");
        setOKButtonText("继续");
        init();
    }

    public void setDescription(String description) {
        this.description = description;
        if (desLabel != null) {
            desLabel.setText(description);
            desLabel.repaint();
        }
    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel jPanel = new JPanel(new BorderLayout());
        desLabel = new JLabel();
        desLabel.setText(description);
        jPanel.add(desLabel, BorderLayout.CENTER);
        return jPanel;
    }

    @Override
    protected void doOKAction() {
        super.doOKAction();
        if (confirmListener != null) {
            confirmListener.onClick(true);
        }
    }

    @Override
    public void doCancelAction() {
        super.doCancelAction();
        if (confirmListener != null) {
            confirmListener.onClick(false);
        }
    }
}
